package com.ansion.basedb.dao.helper;


import com.ansion.basedb.dao.ContactsDao;
import com.ansion.basedb.dao.UserLogicDao;
import com.ansion.basedb.dao.impl.ContactsCacheDaoImpl;
import com.ansion.basedb.dao.model.ContactsModel;
import com.ansion.basedb.dao.model.UserModel;
import com.ansion.basedb.database.DatabaseTools;
import com.ansion.basedb.database.SqlQueryUtil;
import com.ansion.basedb.database.sql.SQLColumn;
import com.ansion.basedb.database.sql.SQLJoinType;
import com.ansion.basedb.storage.StorageSdk;
import com.ansion.utils.ValueSafeTransfer;

import java.util.List;

/**
 * Created by Andre on 23-12-26.
 */
public class ContactsHelper {
    private static ContactsDao contactsDao;

    public static ContactsDao getContactsDao() {
        if (contactsDao == null) {
            synchronized (ContactsDao.class) {
                if (contactsDao == null) {
                    contactsDao = new ContactsCacheDaoImpl();
                }
            }
        }
        return contactsDao;
    }

    public static List<ContactsModel> loadAll(boolean fromDb) {
        ContactsDao dao = getContactsDao();
        if (null != dao) {
            return dao.loadAll(fromDb);
        } else {
            return null;
        }
    }

    public static List<UserModel> loadAllAppUserContact() {
        List<ContactsModel> contactsModels = loadAll(false);
        return ValueSafeTransfer.transformList(contactsModels, (position, contact) -> UserHelper.getUserModel(contact.getUserId()));
    }

    public static ContactsModel getContact(long uid) {
        ContactsDao dao = getContactsDao();
        if (null != dao) {
            return dao.getContact(uid);
        } else {
            return null;
        }
    }


    public static void checkAllContacts(List<ContactsModel> contactList, boolean isFirstLoad) {
        ContactsDao dao = getContactsDao();
        if (null != dao) {
            dao.checkAllContacts(contactList, isFirstLoad);
        }
    }

    public static void setContactLoaded() {
        ContactsDao dao = getContactsDao();
        if (null != dao) {
            dao.setContactLoaded();
        }
    }

    /***
     * 是否是app的用户，注意这个意思代表用户是否注册了我们的应用
     * @param uid
     * @return
     */
    public static boolean isAppUser(long uid) {
        UserLogicDao dao = UserHelper.getUserDBDao();
        if (null != dao) {
            return dao.isAppUser(uid);
        }
        return false;
    }


    public static boolean isContactLoaded() {
        ContactsDao dao = getContactsDao();
        if (null != dao) {
            return dao.isContactLoaded();
        }
        return false;
    }

    /**
     * UserModel ，ContactsModel 联表查询
     *
     * @return
     */
    public static List<ContactQueryBeanForUI> loadAppContacts() {
        String userTableName = DatabaseTools.getTableName(UserModel.class);
        String contactTaleName = DatabaseTools.getTableName(ContactsModel.class);
        String query = "SELECT " + SqlQueryUtil.buildSqlSelect(
                SQLColumn.getColumnName(userTableName, UserModel.kColumnName_UserId),
                SQLColumn.getColumnName(contactTaleName, ContactsModel.kColumnName_ContactId)
        )
                + " FROM " + userTableName
                + SqlQueryUtil.buildSqlJoin(SQLJoinType.TYPE_INNER_JOIN, userTableName, UserModel.kColumnName_UserId,
                contactTaleName, ContactsModel.kColumnName_UserId
        );
        return StorageSdk.getMainDB().select(ContactQueryBeanForUI::new, query);
    }


}
